package com.yz.crm.passport.action;

import com.yz.common.model.RemoteInteractObject;
import com.yz.crm.company.service.CompanyService;
import com.yz.crm.db.model.BaseCompany;
import com.yz.crm.db.model.BaseEmployee;
import com.yz.crm.employee.service.EmployeeService;
import com.yz.crm.passport.constant.PassportConst;
import com.yz.crm.passport.service.PassportService;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 用户注册
 * Created by DOVE on 2017/5/15 0015.
 */
@Controller
@RequestMapping("/passport")
public class RegisterAction {
    private static final Log log = LogFactory.getLog(RegisterAction.class);

    @Resource
    private CompanyService companyService;
    
    @Resource
    private EmployeeService employeeService;
    
    @Resource
    private PassportService passportService;

    /**
     * 注册公司
     *
     * @return
     */
    @RequestMapping("/register")
    private String companyRegister() {
        return "/passport/register";
    }

    /**
     * 注册
     *
     * @param request
     * @param response
     * @param company
     * @return
     */
    @RequestMapping(value = "/doRegister", method = {RequestMethod.POST})
    @ResponseBody
    public RemoteInteractObject doRegister(HttpServletRequest request, HttpServletResponse response, BaseCompany company) {

        RemoteInteractObject ValidResult = new RemoteInteractObject();
        if (company == null) {
            ValidResult.setMsgCode(PassportConst.CODE_FAILURE);
            ValidResult.setMsg("注册失败，请稍后重试");
            return ValidResult;
        }
        try {
            if (companyService.add(company)) {
            	
            	//生成公司的管理员
            	BaseEmployee employee = employeeService.createCompanyAdmin(company);
            	if (employee!=null){
            		request.getSession().setAttribute(PassportConst.SESSION_KEY_USER, passportService.login(employee.getId()));
            		ValidResult.setMsgCode(PassportConst.CODE_SUCCESS);
            		ValidResult.setMsg("注册成功！");
            		ValidResult.setSuccess(true);
            	}else{
            		log.info("创建公司管理员异常");
            		ValidResult.setMsgCode(PassportConst.CODE_FAILURE);
                    ValidResult.setMsg("注册过程中发生了错误，请稍后重试");
            	}

            } else {
                ValidResult.setMsgCode(PassportConst.CODE_FAILURE);
                ValidResult.setMsg("注册失败，请稍后重试");

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ValidResult;
    }

    /**
     * 注册信息验证
     * 判断用户名，公司名名称，和手机号码是否已经被注册
     */
    @RequestMapping(value = "/check")
    @ResponseBody
    public RemoteInteractObject check(String username, String name, String mail, String contactNumber) {
        RemoteInteractObject ValidResult = new RemoteInteractObject();
        try {
            if (StringUtils.isNotEmpty(username)) {
                BaseCompany baseCompany = companyService.getByUsername(username);
                if (baseCompany != null) {
                    ValidResult.setSuccess(false);
                    ValidResult.setMsg("用户名已经存在");
                    return ValidResult;
                }
            }
            if (StringUtils.isNotEmpty(name)) {
                BaseCompany baseCompany = companyService.getByName(name);
                if (baseCompany != null) {
                    ValidResult.setSuccess(false);
                    ValidResult.setMsg("公司名已经存在");
                    return ValidResult;
                }
            }
            if (StringUtils.isNotEmpty(mail)) {
                BaseCompany baseCompany = companyService.getByMail(mail);
                if (baseCompany != null) {
                    ValidResult.setSuccess(false);
                    ValidResult.setMsg("该邮箱已经存在");
                    return ValidResult;
                }
            }
            if (StringUtils.isNotEmpty(contactNumber)) {
                BaseCompany baseCompany = companyService.getPhoneNo(contactNumber);
                if (baseCompany != null) {
                    ValidResult.setSuccess(false);
                    ValidResult.setMsg("该手机号已经存在");
                    return ValidResult;
                }
            }
        } catch (Exception e) {

        }
        ValidResult.setSuccess(true);
        return ValidResult;
    }
}
